package com.ruoyi.framework.web.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.DTO.*;
import com.ruoyi.common.core.domain.entity.ComboCardConfig;
import com.ruoyi.common.core.domain.entity.ComboOrder;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.mapper.ComboCardConfigMapper;
import com.ruoyi.system.mapper.ComboOrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

@Slf4j
@Component
@Transactional
public class ComboOrderService {
    @Autowired
    private ComboOrderMapper comboOrderMapper;
    @Autowired
    private ComboCardConfigService comboCardConfigService;

    //兑换套餐
    public void exchange(ExchangeBO exchangeBO) {
        //根据卡号和卡密查询卡片配置表
        SelectComboCardConfigVO selectComboCardConfigVO = comboCardConfigService.selectByCardNumberAndCardKey(exchangeBO.getCardNumber(), exchangeBO.getCardKey());
        if(ObjectUtils.isEmpty(selectComboCardConfigVO)){
            throw new BusinessException("卡号或卡密错误");
        }
        Integer status = selectComboCardConfigVO.getStatus();
        if(status == 1){
            throw new BusinessException("该卡片已兑换");
        }
        if(status == 2){
            throw new BusinessException("该卡片已作废");
        }
        if(!exchangeBO.getComboId().equals(selectComboCardConfigVO.getComboId())){
         throw new BusinessException("卡号与套餐不匹配");
        }
        ComboOrder comboOrder = new ComboOrder();
        BeanUtils.copyProperties(exchangeBO, comboOrder);
        comboOrder.setComboCardConfigId(selectComboCardConfigVO.getId());
        comboOrderMapper.insertComboOrder(comboOrder);
        //更新卡片状态
        UpdateComboCardConfigBO configBO = new UpdateComboCardConfigBO();
        configBO.setId(selectComboCardConfigVO.getId());
        configBO.setStatus(1);
        configBO.setExchangeTime(new Date());
        comboCardConfigService.updateComboCardConfig(configBO);
    }
    //更新快递单号
    public void updateComboOrder(UpdateComboOrderBO updateComboOrderBO) {
        ComboOrder comboOrder = new ComboOrder();
        BeanUtils.copyProperties(updateComboOrderBO, comboOrder);
        comboOrderMapper.updateComboOrder(comboOrder);
    }

    //用户确认收货
    public void confirmReceipt(ConfirmReceiptBO confirmReceiptBO) {
        ComboOrder comboOrder = new ComboOrder();
        BeanUtils.copyProperties(confirmReceiptBO, comboOrder);
        comboOrderMapper.updateComboOrder(comboOrder);
    }

    //分页查看订单列表
    public PageInfo<SelectComboOrderVO> selectComboOrders(SelectComboOrderBO selectComboOrderBO) {
        Integer pageNum = selectComboOrderBO.getPageNum();
        Integer pageSize = selectComboOrderBO.getPageSize();
        // 配置分页插件后使用
        PageHelper.startPage(pageNum, pageSize);
        List<SelectComboOrderVO> comboOrderVOS = comboOrderMapper.selectComboOrder(selectComboOrderBO);
        return new PageInfo<>(comboOrderVOS);
    }

}
